//
//  RegisterVC.m
//  ShengHuoJia
//
//  Created by xiebin on 2017/12/21.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "RegisterVC.h"
#import "UIButton+CountDown.h"
#define  kSeparateLineHeight 0.7
@interface RegisterVC ()
{
    UITextField *txtf_email;
    UITextField *txtf_code;
    UITextField *txtf_pwd1;
    UITextField *txtf_pwd2;
}
@end

@implementation RegisterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"注册";
    [self initSubViews];
}
-(void)initSubViews{
     self.view.backgroundColor=GET_HEX_COLOR(@"2b3d61");
       self.navView.backgroundColor=[UIColor clearColor];
    UIImageView *logoImagV=[[UIImageView alloc]init];
    [self.view addSubview:logoImagV];
    logoImagV.contentMode=UIViewContentModeScaleAspectFit;
    logoImagV.image=[UIImage imageNamed:@"login_logo"];
    CGFloat whScale=1024.0/242.0;
logoImagV.sd_layout.topSpaceToView(self.view,NAVBAR_HEIGH+20).centerXEqualToView(self.view).widthIs(whScale*40).heightIs(40);
    
    UIImageView *imgv_name = [self createImageViewWithImageName:@"login_username"];
imgv_name.sd_layout.topSpaceToView(logoImagV,30).leftSpaceToView(self.view, 30).widthIs(20).heightIs(20);

    txtf_email = [self createTextFieldWithPlaceHolder:@"请输入邮箱" keyboardType:UIKeyboardTypeDefault];
    txtf_email.sd_layout.leftSpaceToView(imgv_name,5).centerYEqualToView(imgv_name).widthIs(200).heightIs(30);
    
    UIView *view_line1 = [self createLineView];
    view_line1.sd_layout.topSpaceToView(txtf_email,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(kSeparateLineHeight);
    
    UIImageView *imgv_code= [self createImageViewWithImageName:@"login_pwd"];
    imgv_code.sd_layout.topSpaceToView(view_line1,20).leftEqualToView(imgv_name).widthIs(20).heightIs(20);
    
    txtf_code = [self createTextFieldWithPlaceHolder:@"请输入验证码" keyboardType:UIKeyboardTypeNumberPad];
    txtf_code.sd_layout.leftSpaceToView(imgv_code,5).centerYEqualToView(imgv_code).widthIs(120).heightIs(30);
    
   UIButton * btn_getCode = [UIButton new];
    [self.view addSubview:btn_getCode];
    btn_getCode.tag=100;
    [btn_getCode setTitleColor:GET_HEX_COLOR(COLOR_TABBAR_TEXT) forState:UIControlStateNormal];
    btn_getCode.titleLabel.font = GET_SYSTEM_FONT(TEXT_FONT_26);
    [btn_getCode setTitle:@"发送" forState:UIControlStateNormal];
//    btn_getCode.layer.borderWidth=1;
//    btn_getCode.layer.borderColor=[UIColor whiteColor].CGColor;
//    btn_getCode.layer.cornerRadius=5;
//    btn_getCode.layer.masksToBounds=YES;
    [btn_getCode addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
btn_getCode.sd_layout.rightSpaceToView(self.view,30).centerYEqualToView(txtf_code).widthIs(60).heightIs(30);
    
    UIView *view_line2 = [self createLineView];
    view_line2.sd_layout.topSpaceToView(txtf_code,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(kSeparateLineHeight);
    
    UIImageView *imgv_pwd1 = [self createImageViewWithImageName:@"login_pwd"];
    imgv_pwd1.sd_layout.topSpaceToView(view_line2,20).leftEqualToView(imgv_name).widthIs(20).heightIs(20);
    
    txtf_pwd1 = [self createTextFieldWithPlaceHolder:@"请输入登录密码" keyboardType:UIKeyboardTypeDefault];
    txtf_pwd1.secureTextEntry=YES;
    txtf_pwd1.sd_layout.leftSpaceToView(imgv_pwd1,5).centerYEqualToView(imgv_pwd1).widthIs(200).heightIs(30);
    
    UIView *view_line3 = [self createLineView];
    view_line3.sd_layout.topSpaceToView(txtf_pwd1,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(kSeparateLineHeight);
    
    UIImageView *imgv_pwd2 = [self createImageViewWithImageName:@"login_pwd"];
    imgv_pwd2.sd_layout.topSpaceToView(view_line3,20).leftEqualToView(imgv_name).widthIs(20).heightIs(20);
    
    txtf_pwd2 = [self createTextFieldWithPlaceHolder:@"请输入资金密码" keyboardType:UIKeyboardTypeDefault];
    txtf_pwd2.secureTextEntry=YES;
    txtf_pwd2.sd_layout.leftSpaceToView(imgv_pwd2,5).centerYEqualToView(imgv_pwd2).widthIs(200).heightIs(30);
    
    UIView *view_line4 = [self createLineView];
    view_line4.sd_layout.topSpaceToView(txtf_pwd2,5).leftSpaceToView(self.view,30).rightSpaceToView(self.view,30).heightIs(kSeparateLineHeight);
    
    RoundButton *btn_register = [RoundButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:btn_register];
    btn_register.backgroundColor=GET_HEX_COLOR(COLOR_TABBAR_TEXT);
    btn_register.tag=101;
    [btn_register setTitle:@"注册" forState:UIControlStateNormal];
    [btn_register addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    btn_register.sd_layout.topSpaceToView(view_line4,40).heightIs(40).widthIs(280).centerXEqualToView(self.view);
    
//    UIButton *btn_back= [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.view addSubview:btn_back];
//    [btn_back setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
//    btn_back.tag=102;
//    [btn_back addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
//    btn_back.sd_layout.topSpaceToView(self.view,STATUS_HEIGH+7).leftSpaceToView(self.view, 20).heightIs(30).widthEqualToHeight();
    
}
- (UIImageView *)createImageViewWithImageName:(NSString *)imageName  {
    UIImageView *img = [UIImageView new];
      [self.view addSubview:img];
    img.image = [UIImage imageNamed:imageName];
    img.contentMode=UIViewContentModeScaleAspectFit;
    return img;
}
- (UITextField *)createTextFieldWithPlaceHolder:(NSString *)placeHolder  keyboardType:(NSInteger)type{
    UITextField *tf = [UITextField new];
          [self.view addSubview:tf];
    NSAttributedString *attributeStr=[[NSAttributedString alloc]initWithString:placeHolder attributes:@{NSForegroundColorAttributeName:GET_RGB_COLOR(255, 255, 255, 0.4),NSFontAttributeName:GET_SYSTEM_FONT(TEXT_FONT_26)}];
    tf.attributedPlaceholder=attributeStr;
    //tf.placeholder = placeHolder;
     tf.borderStyle = UITextBorderStyleNone;
    //tf.delegate = self;
    //tf.tag = tag;
    tf.font = GET_SYSTEM_FONT(TEXT_FONT_26);
    [tf setTextColor:[UIColor whiteColor]];
    tf.clearButtonMode = UITextFieldViewModeWhileEditing;
    tf.keyboardType = type;
    tf.returnKeyType = UIReturnKeyNext;
    return tf;
}
-(UIView *)createLineView{
    UIView *view_line = UIView.new;
          [self.view addSubview:view_line];
    view_line.backgroundColor = [UIColor whiteColor];
    return view_line;
}
#pragma mark------点击事件
-(void)buttonClick:(UIButton *)sender {
    [self.view endEditing:true];
    if (sender.tag == 100){
        //获取验证码
        DeLog(@"获取验证码");
        //NSMutableDictionary *dic_params = NSMutableDictionary.new;
        NSString *str_email = [NSString stringWithFormat:@"%@",txtf_email.text];
        
        if (![ValidateUtil validateEmail:str_email] || [StringUtils isEmpty:str_email]) {
            //                [Toast makeToast:self.view str:@"您手机号码为空或输入错误!"];
            [WToast showWithText:@"您邮箱为空或输入错误!"];
            return;
        } else {
            [sender mt_startCountTime:60 title:@"获取验证码" waitTittle:@"剩余%ds"];
            [self requestSendVerifyCode];
            //dic_params[@"Mobile"] = str_account;
        }
        
        //        dic_params[@"CodeType"] = @"4";
        //
        //        [HttpManager requestWithProgressTitle:@"加载数据中..." method:HTTPS_POST path:URL_SEND_CODE paramDict:dic_params finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
        //            DeLog(@"%@",result);
        //            [WToast showWithText:@"验证码发送成功,请注意查收"];
        //        }];
    }else if (sender.tag ==101){
        //[self setWebCookie];
        //           AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        //        [app goTabMain];
        //        WebViewVC *vc=[[WebViewVC alloc]init];
        //        [self presentViewController:vc animated:YES completion:nil];
        //        return;
//        if ([StringUtils isEmpty:txtf_name.text] || [StringUtils isEmpty:txtf_mobile1.text]|| [StringUtils isEmpty:txtf_code.text]|| [StringUtils isEmpty:txtf_pwd.text]|| [StringUtils isEmpty:txtf_mobile2.text]) {
//            //                [Toast makeToast:self.view str:@"您手机号码为空或输入错误!"];
//            [WToast showWithText:@"请将信息输入完整"];
//            return;
//        }
        [self requestUserRegister];
        
    }else if (sender.tag ==102){
    [self.navigationController popViewControllerAnimated:YES];
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark-------- 网络请求
-(void)requestSendVerifyCode{
    NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc]init];
    paramsDict[@"map"] =URL_SEND_CODE;
     paramsDict[@"phone"] =txtf_email.text;
     paramsDict[@"scene"] =@"1";
    [HttpManager requestWithProgressTitle:@"" method:HTTPS_GET path:API_PATH paramDict:paramsDict finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
//        NSInteger code=[result[@"code"] integerValue];
//        if (code==200) {
//            [self.navigationController popViewControllerAnimated:YES];
//        }else{
//
//        }
        NSString *message=result[@"msg"];
        [self.view makeToast:message];
    }];
}
-(void)requestUserRegister{
    NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc]init];
    paramsDict[@"map"] =URL_USER_REGISTER;
    paramsDict[@"phone"] =txtf_email.text;
    //paramsDict[@"password"] =txtf_pwd.text;
    paramsDict[@"pushphone"] =txtf_email.text;
    paramsDict[@"code"] =txtf_code.text;
     paramsDict[@"realname"] =txtf_email.text;
    [HttpManager requestWithProgressTitle:@"" method:HTTPS_GET path:API_PATH paramDict:paramsDict finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
        NSInteger code=[result[@"code"] integerValue];
        if (code==200) {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            
        }
        NSString *message=result[@"msg"];
        [WToast showWithText:message];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
